package com.yc.ssq.servlet;

import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UploadServlet", value = "/Upload.do")

@MultipartConfig
public class UploadServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        Part datafile = request.getPart("datafile");
        String filename = datafile.getSubmittedFileName();

        datafile.write("c:/" + filename);
        Result result = new Result(1, "文件上传成功!", null);

        response.setContentType("application/json;charset=utf-8");

        String json = new Gson().toJson(result);

        response.getWriter().print(json);
    }
}
